package com.bit.demo;

import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/request")
public class RequestController {
    @RequestMapping("/login")
    public String login(String name){
        return "name:" + name;
    }

    @RequestMapping("/r1")
    public String r1(Integer age){
        return "age:" + age;
    }

    @RequestMapping("/r2")
    public String r2(int age){
        return "age:" + age;
    }

    @RequestMapping("/r3")
    public String r3(String name, String password){
        return "name:" + name +  ", password:" + password;
    }

    @RequestMapping("/r4")
    public String r4(Person person){
        return "Preson:" + person;
    }

    @RequestMapping("/r5")
    public String r5(@RequestParam(value = "q", required = false) String keyword){
        return "keyword:" + keyword;
    }

    @RequestMapping("/r6")
    public String r6(Integer[] arr){
        return Arrays.toString(arr) + arr.length;
    }

    @RequestMapping("/r7")
    public String r7(@RequestParam("a") List<String> list){
        StringBuilder builder = new StringBuilder();
        for(String s : list){
            builder.append(s).append(",");
        }
        return builder.toString();
    }

    @RequestMapping("/r8")
    public String r8(@RequestBody Person person){
        return person.getName() + person.getAge();
    }

    @RequestMapping("/article/{articleid}")
    public String r9(@PathVariable("articleid") Integer id) {
        return "id:" + id;
    }

    @RequestMapping("/article/{articleid}/{type}")
    public String r10(@PathVariable("articleid") String id,@PathVariable String type){
        return "id:" + id + ", type:" + type;
    }

    @RequestMapping("/r11")
   public String r11(@RequestPart("file111") MultipartFile file) throws IOException {
       String originalFilename = file.getOriginalFilename();
       File destFile = new File("D:\\Downloads\\tmp\\"+originalFilename);
       file.transferTo(destFile);
       return "上传成功";
   }

   @RequestMapping("/r12")
   public String r12(@RequestParam("file222") MultipartFile file) throws IOException {
       String originalFilename = file.getOriginalFilename();
       File destFile = new File("D:\\Downloads\\tmp\\"+originalFilename);
       file.transferTo(destFile);
       return "上传成功";
   }

   @RequestMapping("/getCookie")
   public String getCookie(HttpServletRequest request){
       Cookie[] cookies = request.getCookies();
       if(cookies!=null){
           for(Cookie cookie: cookies){
               System.out.println(cookie.getName() + ":" + cookie.getValue());
           }
       }

       return "获取cookie成功";
   }

   @RequestMapping("/getCookie2")
   public String getCookie(@CookieValue String name){
        return "获取cookie成功" + name;
   }

   @RequestMapping("/setSession")
   public String setSession(HttpServletRequest request){
       HttpSession session = request.getSession();
       session.setAttribute("name","java114");
       session.setAttribute("age",21);
       return "session设置成功";
   }

    @RequestMapping("/getSession")
    public String getSession(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        String name = (String)session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");
        // Integer age = Integer.parseInt((String)session.getAttribute("age"));
        //System.out.println(session.getAttribute("name"));
       // System.out.println(session.getAttribute("age"));
        return "获取session成功" + name + age;
    }

    @RequestMapping("/setSession2")
    public String setSession2(String name,HttpSession session){
        //HttpSession session = request.getSession();
        session.setAttribute("name",name);
        session.setAttribute("age",1);
        return "session设置成功";
    }

    @RequestMapping("/getSession2")
    public String getSession2(HttpSession session){
        String name = (String) session.getAttribute("name");
        Integer age = (Integer) session.getAttribute("age");

        return "name:" + name + ", age:" + age;
    }


    @RequestMapping("/getSession3")
    public String getSession3(@SessionAttribute("name") String name){
        return "name:" + name;
    }

    @RequestMapping("/getHeader")
    public String getHeader(HttpServletRequest request){
        String userAgent = request.getHeader("User-Agent");
        return "User-Agent:" + userAgent;
    }

    @RequestMapping("/getHeader2")
    public String getHeader2(@RequestHeader("User-Agent") String userAgent){
        //String userAgent = request.getHeader("User-Agent");
        return "User-Agent:" + userAgent;
    }
}
